/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.service;


import com.alibaba.fastjson.JSON;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.storage.dao.TransActivityDao;
import com.jeesite.modules.storage.entity.ActivityGoods;
import com.jeesite.modules.storage.entity.ActivityGoodsSpec;
import com.jeesite.modules.storage.entity.TransActivity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 转发活动Service
 */
@Service
@Transactional(readOnly=true)
public class TransActivityService extends CrudService<TransActivityDao, TransActivity> {

	@Autowired
	private ActivityService activityService;

	@Autowired
	private ActivityGoodsService activityGoodsService;

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private ActivityGoodsSpecService activityGoodsSpecService;

	@Autowired
	private TransGoodsService transGoodsService;

	@Autowired
	private TransGoodsSpecService transGoodsSpecService;

	@Autowired
	private BrandFocusService brandFocusService;

	/**
	 * 转发
	 * @param memberId 会员Id
	 * @param type 类型【活动、选择性、商品】
	 * @param activityId 活动Id
	 * @param isBuy 是否购买
	 * @param addPrice 加价
	 * @return 转发Id
	 */
	@Transactional(readOnly=false)
	public String trans(String memberId, String type, String activityId,Boolean isBuy, Double addPrice) throws Exception {
		TransActivity transActivity = new TransActivity();
		transActivity.setMemberId(memberId);
		transActivity.setType(type);
		transActivity.setBrandId(activityService.getBrandIdById(activityId));
		transActivity.setActivityId(activityId);
		transActivity.setIsBuy(isBuy);
		transActivity.setAddPrice(addPrice);
		super.save(transActivity);
		return transActivity.getId();
	}

	/**
	 * 活动转发
	 * @param memberId 会员Id
	 * @param activityId 活动Id
	 * @param isBuy 是否购买
	 * @param addPrice 加价
	 * @return 分享
	 */
	@Transactional(readOnly=false)
	public Map activity(String memberId, String activityId, Boolean isBuy, Double addPrice) throws Exception {
		String type = "活动";
		String transActivityId = trans(memberId,type,activityId,isBuy,addPrice);
		ActivityGoods filterActivityGoods = new ActivityGoods();
		filterActivityGoods.setActivityId(activityId);
		List<ActivityGoods> activityGoodsList = activityGoodsService.findList(filterActivityGoods);
		for (ActivityGoods activityGoods : activityGoodsList){
			String transGoodsId = transGoodsService.trans(memberId,transActivityId,activityGoods,null,isBuy,addPrice);
			ActivityGoodsSpec filterActivityGoodsSpec = new ActivityGoodsSpec();
			filterActivityGoodsSpec.setActivityId(activityId);
			filterActivityGoodsSpec.setGoodsId(activityGoods.getId());
			List<ActivityGoodsSpec> activityGoodsSpecList = activityGoodsSpecService.findList(filterActivityGoodsSpec);
			for (ActivityGoodsSpec activityGoodsSpec : activityGoodsSpecList){
				transGoodsSpecService.trans(memberId,transActivityId,transGoodsId, activityGoodsSpec,null,isBuy,addPrice);
			}
		}
		Map map = activityService.getShareById(activityId);
		map.put("transActivityId",transActivityId);
		return map;
	}

	/**
	 * 选择性转发
	 * @param memberId 会员Id
	 * @param activityId 活动Id
	 * @param isBuy 是否购买
	 * @param addPrice 加价
	 * @return 分享
	 */
	@Transactional(readOnly=false)
	public Map selectivity(String memberId,String activityId,String goods,Boolean isBuy, Double addPrice) throws Exception {
		String type = "选择性";
		String transActivityId = trans(memberId,type,activityId,isBuy,addPrice);
		List<Map> maps = JSON.parseObject(goods, ArrayList.class);
		for(Map map : maps){
			ActivityGoods activityGoods = activityGoodsService.get(String.valueOf(map.get("goodsId"))) ;
			String transGoodsId = transGoodsService.trans(memberId,transActivityId,activityGoods,null,isBuy,addPrice);
			ActivityGoodsSpec filterActivityGoodsSpec = new ActivityGoodsSpec();
			filterActivityGoodsSpec.setActivityId(activityId);
			filterActivityGoodsSpec.setGoodsId(activityGoods.getId());
			List<ActivityGoodsSpec> activityGoodsSpecList = activityGoodsSpecService.findList(filterActivityGoodsSpec);
			for (ActivityGoodsSpec activityGoodsSpec : activityGoodsSpecList){
				transGoodsSpecService.trans(memberId,transActivityId,transGoodsId, activityGoodsSpec,null,isBuy,addPrice);
			}
		}
		Map map = activityService.getShareById(activityId);
		map.put("transActivityId",transActivityId);
		return map;
	}

	/**
	 * 商品转发
	 * @param memberId 会员Id
	 * @param activityId 活动Id
	 * @param goodsId 商品Id
	 * @param method 方式【单图、多图】
	 * @param isBuy 是否购买
	 * @param addPrice 加价
	 * @return 分享
	 */
	@Transactional(readOnly=false)
	public Map goods(String memberId,String activityId, String goodsId, String method, Boolean isBuy, Double addPrice) throws Exception {
		String type = "商品";
		String transActivityId = trans(memberId,type,activityId,isBuy,addPrice);
		ActivityGoods activityGoods = activityGoodsService.get(goodsId) ;
		String transGoodsId = transGoodsService.trans(memberId,transActivityId,activityGoods,method,isBuy,addPrice);
		ActivityGoodsSpec filterActivityGoodsSpec = new ActivityGoodsSpec();
		filterActivityGoodsSpec.setActivityId(activityId);
		filterActivityGoodsSpec.setGoodsId(activityGoods.getId());
		List<ActivityGoodsSpec> activityGoodsSpecList = activityGoodsSpecService.findList(filterActivityGoodsSpec);
		for (ActivityGoodsSpec activityGoodsSpec : activityGoodsSpecList){
			transGoodsSpecService.trans(memberId,transActivityId,transGoodsId, activityGoodsSpec,method,isBuy,addPrice);
		}
		Map map = goodsService.getShareById(goodsId);
		map.put("transActivityId",transActivityId);
		map.put("thumbnail",goodsService.getSharePic(transActivityId,goodsId,activityGoods.getStockNum(), method,isBuy));
		return map;
	}

	/**
	 * 查找转发品牌列表【日】
	 * @param transMemberId 转发会员Id
	 * @param memberId 会员Id
	 * @param dayDate 每日日期
	 * @param size 每页数量
	 * @param page 页码
	 * @return 转发品牌列表
	 */
	public List<Map> getDayBrandList(String transMemberId, String memberId, String dayDate, Integer size, Integer page){
		List<Map> brandMaps = dao.findDayBrandList(memberId,dayDate,size * (page - 1),size);
		for (Map brandMap : brandMaps){
			String brandId = String.valueOf(brandMap.get("brandId"));
			brandMap.put("isFocus",brandFocusService.checkExist(transMemberId,memberId,brandId));
			List<Map> activityMaps = dao.findDayActivitiesByBrandId(brandId,memberId,dayDate);
			for (Map activityMap : activityMaps){
				activityMap.put("dayVisitNum",0);
				activityMap.put("daySaleNum",0);
				activityMap.put("daySaleAmount",0);
				activityMap.put("dayProfit",0);
			}
			brandMap.put("activities",activityMaps);
		}
		return brandMaps;
	}

	/**
	 * 查找转发品牌列表【月】
	 * @param transMemberId 转发会员Id
	 * @param memberId 会员Id
	 * @param monthDate 每月日期
	 * @param size 每页数量
	 * @param page 页码
	 * @return 转发品牌列表
	 */
	public List<Map> getMonthBrandList(String transMemberId, String memberId,String monthDate, Integer size, Integer page){
		List<Map> brandMaps = dao.findMonthBrandList(memberId,monthDate,size * (page - 1),size);
		for (Map brandMap : brandMaps){
			String brandId = String.valueOf(brandMap.get("brandId"));
			brandMap.put("isFocus",brandFocusService.checkExist(transMemberId,memberId,brandId));
			List<Map> activityMaps = dao.findMonthActivitiesByBrandId(brandId,memberId,monthDate);
			for (Map activityMap : activityMaps){
				activityMap.put("dayVisitNum",0);
				activityMap.put("daySaleNum",0);
				activityMap.put("daySaleAmount",0);
				activityMap.put("dayProfit",0);
			}
			brandMap.put("activities",activityMaps);
		}
		return brandMaps;
	}

	/**
	 * 重新转发
	 * @param transActivityId 转发Id
	 * @return 分享
	 */
	@Transactional(readOnly=false)
	public Map again(String transActivityId){
		Map map = new HashMap();
		Map transMap = dao.findById(transActivityId);
		String type = String.valueOf(transMap.get("type"));
		String activityId = String.valueOf(transMap.get("activityId"));
		if(type.equals("活动")){
			map = activityService.getShareById(activityId);
		}else if(type.equals("选择性")){
			map = activityService.getShareById(activityId);
		}else if(type.equals("商品")){
			String goodsId = String.valueOf(transMap.get("goodsId"));
			ActivityGoods activityGoods = activityGoodsService.get(goodsId);
			map = goodsService.getShareById(goodsId);
			map.put("transActivityId",transActivityId);
			map.put("thumbnail",goodsService.getSharePic(transActivityId,goodsId,activityGoods.getStockNum(), String.valueOf(transMap.get("method")),Boolean.parseBoolean(String.valueOf(transMap.get("isBuy")))));
		}
		map.put("transActivityId",transActivityId);
		return map;
	}

	/**
	 * 获取活动Id【Id】
	 * @param transActivityId 转发活动Id
	 * @return 活动Id
	 */
	public String getActivityIdById(String transActivityId){
		return dao.findActivityIdById(transActivityId);
	}
}
